<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="生成报表年月" prop="scny" label-width="100px">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.scny"
          type="month"
          placeholder="生成报表年月"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis_consumption:analysis_consumption:make']"
          >生成</el-button
        >
      </el-form-item> -->
      <!-- <el-form-item label="报表编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入报表编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->

      <el-form-item label="生产年月" prop="scny">
        <el-date-picker
          :editable="false"
          clearable
          size="small"
          v-model="queryParams.scny"
          type="monthrange"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"
          value-format="yyyy-MM"
        >
        </el-date-picker>
      </el-form-item>
      <!--  <el-form-item label="仓储物流中心" prop="storageCenter">
        <el-input
          v-model="queryParams.storageCenter"
          placeholder="请输入仓储物流中心"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="生产部" prop="pd">
        <el-input
          v-model="queryParams.pd"
          placeholder="请输入生产部"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="制表人" prop="zbrPerson">
        <el-input
          v-model="queryParams.zbrPerson"
          placeholder="请输入制表人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['analysis_consumption:analysis_consumption:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['analysis_consumption:analysis_consumption:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['analysis_consumption:analysis_consumption:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['analysis_consumption:analysis_consumption:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="analysis_consumptionList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="id" width="100px" /> -->
      <!-- <el-table-column label="报表编号" align="center" prop="code" /> -->

      <el-table-column label="生产年月" align="center" prop="scny" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.scny, "{y}-{m}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="报表名称" align="center" prop="name" />

      <!-- <el-table-column label="仓储物流中心" align="center" prop="storageCenter" /> -->
      <el-table-column label="节/超价值(元)" align="center" prop="pd" />
      <el-table-column label="制表人" align="center" prop="zbrPerson" />
      <el-table-column label="兑现奖励或扣罚(元)" align="center" prop="zjf" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="excelhandleView(scope.row)"
            >导出</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['analysis_consumption:analysis_consumption:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['analysis_consumption:analysis_consumption:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改原材物料消耗-仰口对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
      custom-class="myDialog"
    >
      <TableHeader
        @nameChange="nameChange"
        :data="{
          mainTitle: '原材物料消耗-仰口',
          company: '青岛崂山矿泉水有限公司',
          code: form.code,
        }"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产年月" prop="scny">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.scny"
                type="month"
                value-format="yyyy-MM"
                placeholder="选择生产年月"
                :disabled="openDisabled"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="报表编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入报表编号"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报表名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入报表名称"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->

          <!--  <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col> -->
        </el-row>
        <el-divider content-position="center"></el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAddRptAnalysisConsumptionSub"
              :disabled="openDisabled"
              >添加</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="handleDeleteRptAnalysisConsumptionSub"
              :disabled="openDisabled"
              >删除</el-button
            >
          </el-col>
        </el-row>
        <el-table
          :summary-method="getSummaries"
          show-summary
          :data="rptAnalysisConsumptionSubList"
          :row-class-name="rowRptAnalysisConsumptionSubIndex"
          @selection-change="handleRptAnalysisConsumptionSubSelectionChange"
          ref="rptAnalysisConsumptionSub"
          fit
          max-height="550"
        >
          <el-table-column type="selection" width="70" align="center" />
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
          />
          <el-table-column label="物料编码" prop="wlbm" minWidth="130">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.wlbm"
                placeholder="请输入"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column>
          <el-table-column label="材料名称 " prop="clmc" minWidth="250">
            <template slot-scope="scope">
              <el-input v-model="scope.row.clmc" :disabled="openDisabled" />
            </template>
          </el-table-column>
          <el-table-column label="理论用量(万个)" prop="llyl" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.llyl"
                placeholder="请输入"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="实际用量(万个)" prop="sjyl" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sjyl"
                placeholder="请输入实际用量(万个)"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="质检耗用量(万个)" prop="zjhyl" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.zjhyl"
                placeholder="请输入质检耗用量(万个)"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="考核指标﹪" prop="khzb" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.khzb"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="实际完成指标﹪" prop="sjwczb" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sjwczb"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="节/超数量(万)" prop="sl" minWidth="150">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.sl"
                placeholder="请输入节/超数量(万)"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="平均价格(元/万)" prop="pjjz" minWidth="120">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.pjjz"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="节/超价值(元)" prop="jz" minWidth="130">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.jz"
                placeholder="请输入节/超价值(元)"
                :disabled="openDisabled"
                @change="compute1(scope)"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="计提节约奖(元)" prop="jyj" minWidth="140">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.jyj"
                placeholder="请输入计提节约奖(元)"
                :disabled="openDisabled"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="扣罚金额(元)" prop="kfje" minWidth="120">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.kfje"
                placeholder="请输入扣罚金额(元)"
                :disabled="openDisabled"
                type="number"
              />
            </template>
          </el-table-column>
          <el-table-column label="兑现奖励或扣罚(元)" prop="dx" minWidth="160">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.dx"
                placeholder="请输入兑现奖励或扣罚(元)"
                :disabled="openDisabled"
                type="number"
              />
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="openDisabled"
              />
            </template>
          </el-table-column> -->
        </el-table>

        <el-row>
          <el-col :span="24">
            <pre>
          备注：（1）价格均不含税；<br />
                （2）计提节约奖=节约价值(合计）*20% ；<br />
                （3）超耗扣罚=超耗价值(合计）*50%（在100%～150%时，按60%；在150%～300%时，按70%；高于300%时，按100%）
            </pre>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="仓储物流中心"
              prop="storageCenter"
              label-width="120px"
            >
              <el-input
                v-model="form.storageCenter"
                placeholder="请输入仓储物流中心"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="生产部" prop="pd">
              <el-input
                v-model="form.pd"
                placeholder="请输入生产部"
                :disabled="openDisabled"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="制表人" prop="zbrPerson">
              <!-- <el-input
                v-model="form.zbrPerson"
                placeholder="请输入制表人"
                :disabled="openDisabled"
              /> -->
              <UserDialog
                @dialogChange="form.zbrPerson = $event.label"
                :echoValue="form.zbrPerson"
                :multiple="false"
                :openDisabled="openDisabled"
                placeholder="请选择人员"
              />
              <!--              <el-select
                v-model="form.zbrPerson"
                placeholder="请选择制表人"
                style="width: 100%"
                  :disabled="openDisabled"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.nickName"
                  :label="item.nickName"
                  :value="item.nickName"
                ></el-option>
              </el-select>-->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysis_consumption,
  getAnalysis_consumption,
  delAnalysis_consumption,
  addAnalysis_consumption,
  updateAnalysis_consumption,
  getData,
} from "@/api/report/analysis/analysis_consumption";
import { getInfo } from "@/api/login";
import { listUser } from "@/api/system/user";

export default {
  name: "Analysis_consumption",
  data() {
    return {
      /* 当前用户名 */
      nickName: "",
      userList: [],
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedRptAnalysisConsumptionSub: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材物料消耗-仰口表格数据
      analysis_consumptionList: [],
      // 原材物料消耗子-仰口表格数据
      rptAnalysisConsumptionSubList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        code: null,
        scny: null,
        scny1: null,
        name: null,
        storageCenter: null,
        pd: null,
        zbrPerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.queryUserList();
  },
  methods: {
    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (index >= 1 && index <= 10) {
          sums[index] = "";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        if (!values.every((value) => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = sums[index].toFixed(2);
        } else {
          sums[index] = sums[index].toFixed(2);
        }
      });

      return sums;
    },
    // 计算
    compute1(scope) {
      const con = this.rptAnalysisConsumptionSubList[scope.$index];
      if (
        con.llyl != "" &&
        con.sjyl != "" &&
        con.zjhyl != "" &&
        con.llyl != "0"
      ) {
        con.sjwczb = (
          ((Number(con.sjyl) - Number(con.llyl) - Number(con.zjhyl)) /
            Number(con.llyl)) *
          100
        ).toFixed(4);
      }
      if (con.sjwczb != "" && con.khzb != "") {
        con.sl = -(
          ((Number(con.sjwczb) - Number(con.khzb)) * Number(con.llyl)) /
          100
        ).toFixed(4);
      }
      if (
        con.sl != "" &&
        con.khzb != "" &&
        con.sjwczb != "" &&
        con.llyl != "" &&
        con.pjjz != ""
      ) {
        //节/超价值（元）
        con.jz =
          (((Number(con.khzb) - Number(con.sjwczb)) * Number(con.llyl)) / 100) *
          Number(con.pjjz).toFixed(0);
      }
      if (con.khzb != "" && con.sjwczb != "") {
        let n = Number(con.sjwczb) - Number(con.khzb);
        if (n < 0) {
          con.jyj = (Number(con.jz) * 0.2).toFixed(2);
        } else {
          con.jyj = "0";
        }
      }

      if (con.khzb != "" && con.sjwczb != "") {
        let m = Number(con.sjwczb) / Number(con.khzb);
        if (m <= 1) {
          con.kfje = "0";
        } else if (m <= 2) {
          con.kfje =
            ((-Number(con.llyl) *
              Number(con.pjjz) *
              (Number(con.sjwczb) - Number(con.khzb))) /
              100) *
            0.5;
        } else if (m <= 2.5) {
          con.kfje =
            ((-Number(con.llyl) * Number(con.pjjz) * Number(con.khzb)) / 100) *
              0.5 -
            ((Number(con.llyl) *
              Number(con.pjjz) *
              (Number(con.sjwczb) - 2 * Number(con.khzb))) /
              100) *
              0.6;
        } else if (m <= 4) {
          con.kfje =
            ((-Number(con.llyl) * Number(con.pjjz) * Number(con.khzb)) / 100) *
              0.5 -
            ((Number(con.llyl) * Number(con.pjjz) * 0.5 * Number(con.khzb)) /
              100) *
              0.6 -
            ((Number(con.llyl) *
              Number(con.pjjz) *
              (Number(con.sjwczb) - 2.5 * Number(con.khzb))) /
              100) *
              0.7;
        } else {
          con.kfje =
            ((-Number(con.llyl) * Number(con.pjjz) * Number(con.khzb)) / 100) *
              0.5 -
            ((Number(con.llyl) * Number(con.pjjz) * 0.5 * Number(con.khzb)) /
              100) *
              0.6 -
            ((Number(con.llyl) * Number(con.pjjz) * 1.5 * Number(con.khzb)) /
              100) *
              0.7 -
            (Number(con.llyl) *
              Number(con.pjjz) *
              (Number(con.sjwczb) - 4 * Number(con.khzb))) /
              100;
        }
      }

      if (con.jyj != "" && con.kfje != "") {
        con.dx = Number(con.jyj) + Number(con.kfje);
      }
    },
    /** 查询原材物料消耗-仰口列表 */
    getList() {
      this.loading = true;
      listAnalysis_consumption(this.queryParams).then((response) => {
        this.analysis_consumptionList = response.rows;
        for (let item of this.analysis_consumptionList) {
          if (typeof item.zjf != "number") {
            item.zjf = Number(item.zjf).toFixed(2);
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        scny: this.queryParams.scny,
        name: "原材物料消耗-仰口",
        storageCenter: null,
        pd: null,
        zbrPerson: this.nickName,
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.rptAnalysisConsumptionSubList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.queryParams.scny != null) {
        let arr = this.queryParams.scny;
        this.queryParams.scny = arr[0];
        this.queryParams.scny1 = arr[1];
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      // console.log(this.queryParams.scny);

      if (this.queryParams.scny == null || this.queryParams.scny == "") {
        this.msgError("请输入生产年月");
        // } else if (Array.isArray(this.queryParams.scny)) {
        //   this.msgError("请重置筛选日期");
      } else {
        let flag = true;
        this.analysis_consumptionList.forEach((item) => {
          if (this.queryParams.scny == item.scny) {
            this.msgError("该日期数据已存在");
            flag = false;
            return;
          }
        });
        if (flag) {
          getData(this.queryParams).then((res) => {
            if (res.length != 0) {
              this.reset();
              this.openDisabled = false;
              this.open = true;
              this.title = "添加原材物料消耗-仰口";

              res.forEach((item) => {
                let obj = {};
                obj.wlbm = item.wlbm;
                obj.clmc = item.clmc;
                obj.llyl = item.llyl;
                obj.sjyl = item.sjyl;
                obj.zjhyl = item.zjhyl;
                obj.khzb = "";
                if (
                  obj.llyl != "" &&
                  obj.sjyl != "" &&
                  obj.zjhyl != "" &&
                  obj.llyl != "0"
                ) {
                  obj.sjwczb = (
                    ((Number(obj.sjyl) - Number(obj.llyl) - Number(obj.zjhyl)) /
                      Number(obj.llyl)) *
                    100
                  ).toFixed(4);
                }
                // obj.sjwczb = "";
                obj.sl = "";
                obj.pjjz = "";
                obj.jz = "";
                obj.jyj = "";
                obj.kfje = "";
                obj.dx = "";
                obj.remark = "";
                this.rptAnalysisConsumptionSubList.push(obj);
              });
              this.handleQuery();
            } else {
              this.msgError("该月份无数据！");
            }
          });
        }
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getAnalysis_consumption(id).then((response) => {
        this.form = response.data;

        var datas = response.data.rptAnalysisConsumptionSubList;
        datas.forEach(function (item, index) {
          item.dx = Number(item.dx).toFixed(2);
          item.kfje = Number(item.kfje).toFixed(2);
          item.pjjz = Number(item.pjjz).toFixed(0);

          item.jz = Number(item.jz).toFixed(2);
          item.jyj = Number(item.jyj).toFixed(2);

          item.llyl = Number(item.llyl).toFixed(4);
          item.sjyl = Number(item.sjyl).toFixed(4);
          item.zjhyl = Number(item.zjhyl).toFixed(4);
          item.khzb = Number(item.khzb).toFixed(4);
          item.sjwczb = Number(item.sjwczb).toFixed(4);
          item.sl = Number(item.sl).toFixed(4);
          // console.log(item); //输出数组的每一个元素
        });
        this.rptAnalysisConsumptionSubList = datas;

        this.open = true;
        this.title = "修改原材物料消耗-仰口";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getAnalysis_consumption(id).then((response) => {
        this.form = response.data;

        var datas = response.data.rptAnalysisConsumptionSubList;
        datas.forEach(function (item, index) {
          item.dx = Number(item.dx).toFixed(2);
          item.kfje = Number(item.kfje).toFixed(2);
          item.pjjz = Number(item.pjjz).toFixed(0);

          item.jz = Number(item.jz).toFixed(2);
          item.jyj = Number(item.jyj).toFixed(2);

          item.llyl = Number(item.llyl).toFixed(4);
          item.sjyl = Number(item.sjyl).toFixed(4);
          item.zjhyl = Number(item.zjhyl).toFixed(4);
          item.khzb = Number(item.khzb).toFixed(4);
          item.sjwczb = Number(item.sjwczb).toFixed(4);
          item.sl = Number(item.sl).toFixed(4);
          // console.log(item); //输出数组的每一个元素
        });
        this.rptAnalysisConsumptionSubList = datas;

        // this.rptAnalysisConsumptionSubList =
        //   response.data.rptAnalysisConsumptionSubList;
        this.open = true;
        this.title = "查看原材物料消耗-仰口";
      });
    },
    summation() {
      let i = 0;
      this.rptAnalysisConsumptionSubList.forEach((item) => {
        if (item.wlbm == "合计") {
          i++;
        }
      });
      if (i == 0) {
        let obj = {};
        obj.wlbm = "合计";
        obj.clmc = "";
        obj.llyl = "";
        obj.sjyl = "";
        obj.zjhyl = "";
        obj.khzb = "";
        obj.sjwczb = "";
        obj.sl = "";
        obj.pjjz = "";
        obj.jz = "";
        obj.jyj = "";
        obj.kfje = "";
        obj.dx = "";
        obj.remark = "";
        this.rptAnalysisConsumptionSubList.forEach((item) => {
          obj.llyl = Number(obj.llyl) + Number(item.llyl);
          obj.sjyl = Number(obj.sjyl) + Number(item.sjyl);
          obj.zjhyl = Number(obj.zjhyl) + Number(item.zjhyl);
          obj.sl = Number(obj.sl) + Number(item.sl);
          obj.jz = Number(obj.jz) + Number(item.jz);
          obj.jyj = Number(obj.jyj) + Number(item.jyj);
          obj.kfje = Number(obj.kfje) + Number(item.kfje);
          obj.dx = Number(obj.dx) + Number(item.dx);
        });
        // this.rptAnalysisConsumptionSubList.push(obj);
      }
    },
    /** 提交按钮 */
    submitForm() {
      // 计算合计
      this.summation();

      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.rptAnalysisConsumptionSubList =
            this.rptAnalysisConsumptionSubList;
          if (this.form.id != null) {
            updateAnalysis_consumption(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const rLoading = this.openLoading();
            addAnalysis_consumption(this.form)
              .then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              })
              .catch((error) => {
                rLoading.close();
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除原材物料消耗-仰口编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysis_consumption(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 原材物料消耗子-仰口序号 */
    rowRptAnalysisConsumptionSubIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 原材物料消耗子-仰口添加按钮操作 */
    handleAddRptAnalysisConsumptionSub() {
      let obj = {};
      obj.wlbm = "";
      obj.clmc = "";
      obj.llyl = "";
      obj.sjyl = "";
      obj.zjhyl = "";
      obj.khzb = "";
      obj.sjwczb = "";
      obj.sl = "";
      obj.pjjz = "";
      obj.jz = "";
      obj.jyj = "";
      obj.kfje = "";
      obj.dx = "";
      obj.remark = "";
      this.rptAnalysisConsumptionSubList.push(obj);
    },
    /** 原材物料消耗子-仰口删除按钮操作 */
    handleDeleteRptAnalysisConsumptionSub() {
      if (this.checkedRptAnalysisConsumptionSub.length == 0) {
        this.$alert("请先选择要删除的原材物料消耗子-仰口数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.rptAnalysisConsumptionSubList.splice(
          this.checkedRptAnalysisConsumptionSub[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleRptAnalysisConsumptionSubSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.rptAnalysisConsumptionSub.clearSelection();
        this.$refs.rptAnalysisConsumptionSub.toggleRowSelection(
          selection.pop()
        );
      } else {
        this.checkedRptAnalysisConsumptionSub = selection;
      }
    },
    excelhandleView(row) {
      this.download(
        "productionProcess/analysis_consumption/exportexcel?id=" + row.id,
        {
          ...this.queryParams,
        },
        "原材物料消耗" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "analysis_consumption/analysis_consumption/export",
        {
          ...this.queryParams,
        },
        "原材物料消耗-仰口" + this.parseTime(new Date()) + ".xlsx"
      );
    },
    //接收编号
    nameChange(e) {
      this.form.code = e;
    },
    /* 查询用户列表 */
    queryUserList() {
      listUser().then((response) => {
        this.userList = response.rows;
      });
      getInfo().then((response) => {
        this.nickName = response.user.nickName;
      });
    },
  },
};
</script>
